package Model;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;

import MailGenerator.Mailer;
import Model.Wall.Post;
import Model.InboxMessages.Message;
import Model.InboxMessages.SimpleMessage;
import DBAccess.DBAccess;

public class Notification {
	public int getUserID() {
		return userID;
	}
	public void setUserID(int userID) {
		this.userID = userID;
	}
	public int getFromID() {
		return fromID;
	}
	public void setFromID(int fromID) {
		this.fromID = fromID;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	public int getChecked() {
		return checked;
	}
	public void setChecked(int checked) {
		this.checked = checked;
	}

	private int  userID;
	private int fromID;
	private String msg;
	private String date;
	private int checked;
	
	public Notification(int userid, int fromid, String msg, Date d)
	{
		this.userID = userid;
		this.msg = msg;
		this.date = d.toLocaleString();
		this.fromID = fromid;
	}

  final private static String EMAIL_SUBJECT = "New notification for UBCDating";

  public static String messageNotificationMessage(int fromID){
	  return String.format("You got a new message from %s.",User.getName(fromID));
  }
  public static String friendNotificationMessage(int userID){
		 return String.format("%s added you as a friend.", User.getName(userID));
}
  public static String flirtNotificationMessage(int userID){
		 return String.format("%s flirted with you.", User.getName(userID));
}
  public static String postNotificationMessage(int postID){
	return String.format("%s has wrote on your wall.", Post.getMessageFromPostID(postID));
  }
  public static int getNotificationFlag(int userID){
	  PreparedStatement ps = null;
	  ResultSet rs = null;
	  int email = 0;
	  try{
		  ps = DBAccess.con.prepareStatement("SELECT notificationflag FROM personaldb WHERE userID=?");
		  ps.setInt(1, userID);
		  rs = ps.executeQuery();
		  
		  if(rs.next())
			  email = rs.getInt(1);
		  DBAccess.close(ps,rs);
	  } catch(Exception e){
		  e.printStackTrace();
	  }
	  return email;
  }
  public static void newNotification(int userID, String message, int fromID){
	  DBAccess.insertNotification(userID, message, fromID);
	  PreparedStatement ps = null;
	  ResultSet rs = null;
	  boolean email = false;
	  try{
		  ps = DBAccess.con.prepareStatement("SELECT notificationflag FROM personaldb WHERE userID=?");
		  ps.setInt(1, userID);
		  rs = ps.executeQuery();
		  
		  if(rs.next())
			  email = rs.getInt(1)==1;
		  DBAccess.close(ps,rs);
	  } catch(Exception e){
		  e.printStackTrace();
	  }

	  if(email){
		  try {
			ps = DBAccess.con.prepareStatement("SELECT email FROM accountdb WHERE userID=?");
			ps.setInt(1, userID);
			rs = ps.executeQuery();
	  
			if(rs.next()){
				String address = rs.getString(1);
				Mailer.sendMail(address, EMAIL_SUBJECT, message);
			}
			DBAccess.close(ps, rs);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	  }
  }
  
  public static ArrayList<Notification> getNotifications(int userID){
	  new DBAccess();
	  ArrayList<Notification> notificationList = new ArrayList<Notification>();
	  PreparedStatement ps = null;
	  ResultSet rs = null;
	  String query = "SELECT * from notificationsdb where userID=? ORDER BY time DESC";
	  try {
		ps = DBAccess.con.prepareStatement(query);
		ps.setInt(1, userID);
		System.out.println(ps.toString());
		rs = ps.executeQuery();
		while(rs.next()){
			System.out.println("RESULT\n");
			int fromID = rs.getInt("fromid");
			String message = rs.getString("message");
			Date time = rs.getDate("time");

			Notification not = new Notification(userID, fromID, message, time);
			notificationList.add(not);
		}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

	  DBAccess.close(ps,rs);

	  return notificationList;
	  
  }
  public static boolean checkNew(int userID){
	  boolean notification = false;
	  PreparedStatement ps = null;
	  ResultSet rs = null;
	  try{
		  ps = DBAccess.con.prepareStatement("SELECT checked FROM notificationsdb WHERE userID=? AND checked=0");
		  ps.setInt(1, userID);
		  rs = ps.executeQuery();
		  
		  if(rs.next())
			  notification = true;
	  } catch(Exception e){
		  e.printStackTrace();
	  }
	  DBAccess.close(ps, rs);
	  return notification;
  }
  public static void checkNotifications(int userID){
	  PreparedStatement ps = null;
	  ResultSet rs = null;
	  try{
		  ps = DBAccess.con.prepareStatement("UPDATE notificationsdb SET checked=1 WHERE userID=? AND checked=0");
		  ps.setInt(1, userID);
		  rs = ps.executeQuery();
	  } catch(Exception e){
		  e.printStackTrace();
	  }
	  DBAccess.close(ps, rs);
  }
}